package com.example.gzysmartcity.ui.fragment.traffic;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.blankj.utilcode.util.ToastUtils;
import com.bumptech.glide.Glide;
import com.example.gzysmartcity.R;
import com.example.gzysmartcity.databinding.FragmentBuscontentBinding;
import com.example.gzysmartcity.repository.model.MyCityBus;
import com.example.gzysmartcity.ui.adapter.BusContentAdapter;
import com.example.gzysmartcity.ui.base.BaseFragment;
import com.example.gzysmartcity.viewmodel.BusListViewModel;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;

public class BusContentFragment extends BaseFragment<FragmentBuscontentBinding> {

    @Override
    protected FragmentBuscontentBinding viewBinding() {
        return FragmentBuscontentBinding.inflate(getLayoutInflater(), baseBinding.getRoot(), true);
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onViewCreated(@NonNull @NotNull View view, @Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        BusListViewModel buslistViewModel = new ViewModelProvider(this).get(BusListViewModel.class);
        Glide.with(requireContext()).load(R.drawable.businset).into(b.imageBusContent);
        Bundle bundle = getArguments();
        if (bundle == null) return;
        MyCityBus myCityBus = (MyCityBus) bundle.getSerializable("buslist");
        buslistViewModel.getCityBusLine(myCityBus.getLineId());
        b.tvPeople.setText("剩余搭载人数: " + myCityBus.getCapacity() + "人");
        b.tvNowZhan.setText("当前站点: " + myCityBus.getNow());
        if (myCityBus.getNext() != null)
            b.tvNextZhan.setText("下一站: " + myCityBus.getNext());
        b.tvDistance.setText("距离: " + myCityBus.getDistance() + "米");
        if (myCityBus.getDirection() == 1) {
            b.tvDirection.setText("车辆方向: 往终点站方向");
        } else {
            b.tvDirection.setText("车辆方向: 往起点站方向");
        }
        ArrayList<String> busStopNameList = myCityBus.getBusStopNameList();
        BusContentAdapter adapter = new BusContentAdapter(R.layout.rv_item_busline, busStopNameList.size());
        adapter.setNewInstance(busStopNameList);
        b.rvBuscontent.setOverScrollMode(View.OVER_SCROLL_NEVER);
        b.rvBuscontent.setAdapter(adapter);
        b.rvBuscontent.setLayoutManager(new LinearLayoutManager(getContext()));
        buslistViewModel.getBusLivedata().observe(getViewLifecycleOwner(), state -> {
            switch (state) {
                case FAIL:
                    ToastUtils.showShort("加载失败");
                    break;
                case SUCCESS:
                    b.tvCircuit.setText("运行线路: " + buslistViewModel.getLineName());
                    break;
            }
        });
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.i("TAG", "onDestroy: ");
    }

    @Override
    protected String setTitle() {
        return "公交详情";
    }

    @Override
    protected boolean showBack() {
        return true;
    }

    @Override
    protected Boolean showBottom() {
        return false;
    }
}
